package com.anolesoft.yr.mbus.pre.service.impl;

import com.anolesoft.yr.mbus.pre.dto.PreOrderUpdateDTO;
import com.anolesoft.yr.mbus.pre.mapper.PreOrderMapper;
import com.anolesoft.yr.mbus.pre.service.PreOrderService;
import com.anolesoft.yr.mbus.pre.vo.PreOrderQueryVO;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import java.util.concurrent.TimeoutException;

@Service
public class PreOrderServiceImpl implements PreOrderService {

    private static final String SUCCESS = "SUCCESS";
    private static final String EXCEPTION = "EXCEPTION";

    @Autowired
    PreOrderMapper preOrderMapper;

    //生成报价单
    @Override
    @Transactional
    public String updatePreOrderState1(PreOrderUpdateDTO preOrderUpdateDTO) throws Exception {
        try {
            int count = preOrderMapper.updatePreOrderState1(preOrderUpdateDTO.getObjuid());
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }

    //查询报价单信息（审批）
    @Override
    public List<PreOrderQueryVO> queryPreOrderAll2(String preInquiryUid) throws Exception {
        List<PreOrderQueryVO> preOrderQueryVOList = preOrderMapper.queryPreOrderAll2(preInquiryUid);
        return preOrderQueryVOList;
    }

    //报价单审批通过
    @Override
    @Transactional
    public String updatePreOrderState3(PreOrderUpdateDTO preOrderUpdateDTO) throws Exception {
        try {
            int count = preOrderMapper.updatePreOrderState3(preOrderUpdateDTO.getObjuid());
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }

    //报价单审批驳回
    @Override
    @Transactional
    public String updatePreOrderState2(PreOrderUpdateDTO preOrderUpdateDTO) throws Exception {
        try {
            int count = preOrderMapper.updatePreOrderState2(preOrderUpdateDTO.getObjuid());
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }

    //查询报价单信息（导出）
    @Override
    public List<PreOrderQueryVO> queryPreOrderAll3(String preInquiryUid) throws Exception {
        List<PreOrderQueryVO> preOrderQueryVOList = preOrderMapper.queryPreOrderAll3(preInquiryUid);
        return preOrderQueryVOList;
    }

    //报价单导出
    @Override
    @Transactional
    public String updatePreOrderState4(PreOrderUpdateDTO preOrderUpdateDTO) throws Exception {
        try {
            PreOrderServiceImpl.expPreOrderPdf("F:/");


            int count = preOrderMapper.updatePreOrderState4(preOrderUpdateDTO.getObjuid());
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }





    //报价单导出到指定位置
    public static String expPreOrderPdf(String filePath) throws Exception {
        System.out.println("开始执行导出...");

        String fileName = "";

        Font headFont = new Font(BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED), Font.BOLD);
        headFont.setSize(20);
        Font groupFont = new Font(BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED), Font.BOLD);
        groupFont.setSize(12);
        Font titleFont = new Font(BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED), Font.BOLD);
        titleFont.setSize(8);
        Font contentFont = new Font(BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED));
        contentFont.setSize(6);

        Document document = new Document(PageSize.A4.rotate());
        PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(filePath + fileName));
        //打开文件
        document.open();
        // 3列的表.
        PdfPTable table = new PdfPTable(17);
        table.setWidthPercentage(100); // 宽度100%填充
        table.setSpacingBefore(10f); // 前间距
        table.setSpacingAfter(10f); // 后间距

        List<PdfPRow> listRow = table.getRows();
        //设置列宽
        float[] columnWidths = {10f, 10f, 10f, 10f, 10f, 10f, 10f, 10f, 10f, 10f, 10f, 10f, 10f, 10f, 10f, 10f, 10f};
        table.setWidths(columnWidths);

        //行1
        PdfPCell cells1[] = new PdfPCell[17];
        PdfPRow row1 = new PdfPRow(cells1);
        cells1[0] = new PdfPCell(new Phrase("报价单", headFont));//单元格内容
        cells1[0].setColspan(17);
        cells1[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
        cells1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
        cells1[0].setMinimumHeight(30);

        //行2
        PdfPCell cells2[] = new PdfPCell[17];
        PdfPRow row2 = new PdfPRow(cells2);
        cells2[0] = new PdfPCell(new Phrase("", groupFont));
        cells2[1] = new PdfPCell(new Phrase("", groupFont));
        cells2[2] = new PdfPCell(new Phrase("配件信息", groupFont));
        cells2[2].setColspan(4);
        cells2[2].setHorizontalAlignment(Element.ALIGN_CENTER);
        cells2[2].setVerticalAlignment(Element.ALIGN_MIDDLE);
        cells2[6] = new PdfPCell(new Phrase("产品服务内容", groupFont));
        cells2[6].setColspan(11);
        cells2[6].setHorizontalAlignment(Element.ALIGN_CENTER);
        cells2[6].setVerticalAlignment(Element.ALIGN_MIDDLE);
        cells2[0].setMinimumHeight(25);

        //行3
        PdfPCell cells3[] = new PdfPCell[17];
        PdfPRow row3 = new PdfPRow(cells3);
        cells3[0] = new PdfPCell(new Phrase("序号", titleFont));
        cells3[1] = new PdfPCell(new Phrase("产品名称", titleFont));
        cells3[2] = new PdfPCell(new Phrase("配件型号", titleFont));
        cells3[3] = new PdfPCell(new Phrase("数量", titleFont));
        cells3[4] = new PdfPCell(new Phrase("询价价格", titleFont));
        cells3[5] = new PdfPCell(new Phrase("报价价格", titleFont));
        cells3[6] = new PdfPCell(new Phrase("产品套餐", titleFont));
        cells3[7] = new PdfPCell(new Phrase("服务项名称", titleFont));
        cells3[8] = new PdfPCell(new Phrase("服务项参数", titleFont));
        cells3[9] = new PdfPCell(new Phrase("服务项名称", titleFont));
        cells3[10] = new PdfPCell(new Phrase("服务项参数", titleFont));
        cells3[11] = new PdfPCell(new Phrase("服务项名称", titleFont));
        cells3[12] = new PdfPCell(new Phrase("服务项参数", titleFont));
        cells3[13] = new PdfPCell(new Phrase("服务项名称", titleFont));
        cells3[14] = new PdfPCell(new Phrase("服务项参数", titleFont));
        cells3[15] = new PdfPCell(new Phrase("服务项名称", titleFont));
        cells3[16] = new PdfPCell(new Phrase("服务项参数", titleFont));
        cells3[0].setMinimumHeight(20);
        for(int i=0; i<=16; i++){
            cells3[i].setHorizontalAlignment(Element.ALIGN_CENTER);
            cells3[i].setVerticalAlignment(Element.ALIGN_MIDDLE);
        }

        //行4
        PdfPCell cells4[] = new PdfPCell[17];
        PdfPRow row4 = new PdfPRow(cells4);
        cells4[0] = new PdfPCell(new Phrase("401", contentFont));
        cells4[1] = new PdfPCell(new Phrase("A", contentFont));
        cells4[2] = new PdfPCell(new Phrase(",", contentFont));
        cells4[3] = new PdfPCell(new Phrase("#", contentFont));
        cells4[4] = new PdfPCell(new Phrase("啊", contentFont));
        cells4[5] = new PdfPCell(new Phrase("406", contentFont));
        cells4[6] = new PdfPCell(new Phrase("407", contentFont));
        cells4[7] = new PdfPCell(new Phrase("408", contentFont));
        cells4[8] = new PdfPCell(new Phrase("409", contentFont));
        cells4[9] = new PdfPCell(new Phrase("410", contentFont));
        cells4[10] = new PdfPCell(new Phrase("411", contentFont));
        cells4[11] = new PdfPCell(new Phrase("412", contentFont));
        cells4[12] = new PdfPCell(new Phrase("413", contentFont));
        cells4[13] = new PdfPCell(new Phrase("414", contentFont));
        cells4[14] = new PdfPCell(new Phrase("415", contentFont));
        cells4[15] = new PdfPCell(new Phrase("416", contentFont));
        cells4[16] = new PdfPCell(new Phrase("417", contentFont));
        cells4[0].setMinimumHeight(15);
        for(int i=0; i<=16; i++){
            cells4[i].setHorizontalAlignment(Element.ALIGN_CENTER);
            cells4[i].setVerticalAlignment(Element.ALIGN_MIDDLE);
        }

        //行5
        PdfPCell cells5[] = new PdfPCell[17];
        PdfPRow row5 = new PdfPRow(cells5);
        cells5[0] = new PdfPCell(new Phrase("序号", contentFont));
        cells5[1] = new PdfPCell(new Phrase("产品名称", contentFont));
        cells5[2] = new PdfPCell(new Phrase("配件型号", contentFont));
        cells5[3] = new PdfPCell(new Phrase("数量", contentFont));
        cells5[4] = new PdfPCell(new Phrase("询价价格", contentFont));
        cells5[5] = new PdfPCell(new Phrase("报价价格", contentFont));
        cells5[6] = new PdfPCell(new Phrase("产品套餐", contentFont));
        cells5[7] = new PdfPCell(new Phrase("服务项名称", contentFont));
        cells5[8] = new PdfPCell(new Phrase("服务项参数", contentFont));
        cells5[9] = new PdfPCell(new Phrase("服务项名称", contentFont));
        cells5[10] = new PdfPCell(new Phrase("服务项参数", contentFont));
        cells5[11] = new PdfPCell(new Phrase("服务项名称", contentFont));
        cells5[12] = new PdfPCell(new Phrase("服务项参数", contentFont));
        cells5[13] = new PdfPCell(new Phrase("服务项名称", contentFont));
        cells5[14] = new PdfPCell(new Phrase("服务项参数", contentFont));
        cells5[15] = new PdfPCell(new Phrase("服务项名称", contentFont));
        cells5[16] = new PdfPCell(new Phrase("服务项参数", contentFont));
        cells5[0].setMinimumHeight(15);
        for(int i=0; i<=16; i++){
            cells5[i].setHorizontalAlignment(Element.ALIGN_CENTER);
            cells5[i].setVerticalAlignment(Element.ALIGN_MIDDLE);
        }

        //把第一行添加到集合
        listRow.add(row1);
        listRow.add(row2);
        listRow.add(row3);
        listRow.add(row4);
        listRow.add(row5);

        //把表格添加到文件中
        document.add(table);
        //关闭文档
        document.close();
        //关闭书写器
        pdfWriter.close();

        System.out.println("导出执行完成...");
        return null;
    }





    //报价单生效
    @Override
    @Transactional
    public String updatePreOrderState6(PreOrderUpdateDTO preOrderUpdateDTO) throws Exception {
        try {
            int count = preOrderMapper.updatePreOrderState6(preOrderUpdateDTO.getObjuid());
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }

    //报价单关闭
    @Override
    @Transactional
    public String updatePreOrderState7(PreOrderUpdateDTO preOrderUpdateDTO) throws Exception {
        try {
            int count = preOrderMapper.updatePreOrderState7(preOrderUpdateDTO.getObjuid());
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }

}
